<?php 
namespace xfyzf\api\controller;
class WxApi{
    public function wxpay_zt_Out(){
        global $date;
        $this->verifyip(1);
        $post         = input();
        $intl_wx_name = $post['intl_wx_name'];
        $wx_name      = $post['wx_name'];
        $sql = "update `pay_qrlist` set `status`=:status,`endtime`=:date,`wx_name`=:wxname WHERE `beizhu`=:beizhu";
        $params = array(
            'status'=>1,
            'date'=>$date,
            'wxname'=>$intl_wx_name,
            'beizhu'=>$wx_name
            );
        if(safeSqlQuery($sql, $params,'update')){
            exitjson(1,'更新状态成功'.$wx_name);
        }
        exitjson(-1,'更新微信状态失败,可能不存在此微信二维码');
    }
    public function wxpay_statuszt(){
        global $date;
        $this->verifyip(1);
        $post         = input();
        $wx_name      = $post['wx_name'];
        $wx_name      = urldecode($wx_name);
        $status       = $post['status'];
        if(empty($wx_name))exitjson(-1,'微信名不可为空');
        $sql = "update `pay_wechat_trumpet` set `status`=:status WHERE `wx_name`=:wx_name";
        $params = array(
            'status'=>1,
            'wx_name'=>$wx_name
            );
        if(safeSqlQuery($sql, $params,'update')){
            exitjson(200,'更新成功');
        }
        exitjson(-1,'更新失败！已是在线状态');
    }
    public function Up_Wechat_Trumpet(){
        $this->verifyip();
        $login_time = time()+60;
        $intl_wx_name = input()['intl_wx_name'];
        $intl_wx_name=urldecode($intl_wx_name);
        $sql = "update `pay_wechat_trumpet` set `login_time`=:login_time WHERE `wx_name`=:wx_name";
        $params = array(
            'login_time'=>$login_time,
            'wx_name'=>$intl_wx_name
            );
        if(safeSqlQuery($sql, $params,'update')){
            exit('更新微信心跳成功：'.$intl_wx_name);
        }else{
            exit('更新微信心跳失败,可能不存在此微信店员小号');
        }
    }
    public function FREEWXPAY(){
        $this->verifyip(1);
        $post   = input();
        $name   = $post['name'];
        $money  = $post['money'];
        if(empty($name) || empty($money))exit('参数不齐全');
        $sql = "SELECT * FROM `pay_qrlist` WHERE `beizhu`='$name' limit 1";
        $qr_row =  executeSQLQuery($sql);
        $pid  = $qr_row['pid'];
        $time = time();
        $E_type = '微信店员';
        $sql  = "SELECT * FROM pay_order WHERE status='0' and pid='{$pid}' and type='wxpay' and price='{$money}' and outtime>'{$time}' limit 1";
        $srow = executeSQLQuery($sql);
        if($srow){
            if($srow['status'] == 0){
                $xf_pid=$srow['pid'];
				$xf_money=$srow['price'];
				$url=creat_callback($srow);
				$datm=get_curl($url['notify']);
				xfhtemail($xf_pid,$xf_money,$E_type,$srow['name'],$srow['trade_no']); 
				createLog($pid,'自动回调订单('.$E_type.')：'.$srow['trade_no'],http_ip());
				exitjson(200,'回调成功！',array("order"=>$srow['trade_no'],"money"=>$xf_money));
            }
        }else{
            exitjson(-1,'此金额匹配不到金额');
        }
        
    }
    private function verifyip($send = false){
         global $conf;
         $ips = explode(',',$conf['wxip']);
         if(!in_array(http_ip(),$ips)){
             if($send)exitjson(-1,"IP验证不通过".http_ip()."，请在后台计划任务中添加白名单");
             exit("IP验证不通过".http_ip()."，请在后台计划任务中添加白名单");
         }
    }
}


?>